package com.shengzai.flink.tf

import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time

object Demo6Window {
  def main(args: Array[String]): Unit = {
    //创建flink环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置时间属性为处理时间
    env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime)

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    /**
     * 统计单词的数量，每隔5秒统计最近15秒额数据
     */
    val kvDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))

    val countDS: DataStream[(String, Int)] = kvDS
      .keyBy(_._1)
      //划分窗口
      .timeWindow(Time.seconds(15), Time.seconds(5))
      .sum(1)

    countDS.print()

    env.execute()


  }
}
